package cn.iocoder.yudao.module.ao.service.majordirection;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ao.controller.admin.majordirection.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.majordirection.MajorDirectionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ao.dal.mysql.majordirection.MajorDirectionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ao.enums.ErrorCodeConstants.*;

/**
 * 专业方向 Service 实现类
 *
 * @author lfl
 */
@Service
@Validated
public class MajorDirectionServiceImpl implements MajorDirectionService {

    @Resource
    private MajorDirectionMapper majorDirectionMapper;

    @Override
    public Long createMajorDirection(MajorDirectionSaveReqVO createReqVO) {
        // 校验专业ID的有效性
        validateParentMajorDirection(null, createReqVO.getMajorId());
        // 校验专业方向名称的唯一性
        validateMajorDirectionDirectionNameUnique(null, createReqVO.getMajorId(), createReqVO.getDirectionName());

        // 插入
        MajorDirectionDO majorDirection = BeanUtils.toBean(createReqVO, MajorDirectionDO.class);
        majorDirectionMapper.insert(majorDirection);

        // 返回
        return majorDirection.getId();
    }

    @Override
    public void updateMajorDirection(MajorDirectionSaveReqVO updateReqVO) {
        // 校验存在
        validateMajorDirectionExists(updateReqVO.getId());
        // 校验专业ID的有效性
        validateParentMajorDirection(updateReqVO.getId(), updateReqVO.getMajorId());
        // 校验专业方向名称的唯一性
        validateMajorDirectionDirectionNameUnique(updateReqVO.getId(), updateReqVO.getMajorId(), updateReqVO.getDirectionName());

        // 更新
        MajorDirectionDO updateObj = BeanUtils.toBean(updateReqVO, MajorDirectionDO.class);
        majorDirectionMapper.updateById(updateObj);
    }

    @Override
    public void deleteMajorDirection(Long id) {
        // 校验存在
        validateMajorDirectionExists(id);
        // 校验是否有子专业方向
        if (majorDirectionMapper.selectCountByMajorId(id) > 0) {
            throw exception(MAJOR_DIRECTION_EXITS_CHILDREN);
        }
        // 删除
        majorDirectionMapper.deleteById(id);
    }


    private void validateMajorDirectionExists(Long id) {
        if (majorDirectionMapper.selectById(id) == null) {
            throw exception(MAJOR_DIRECTION_NOT_EXISTS);
        }
    }

    private void validateParentMajorDirection(Long id, Long majorId) {
        if (majorId == null || MajorDirectionDO.MAJOR_ID_ROOT.equals(majorId)) {
            return;
        }
        // 1. 不能设置自己为父专业方向
        if (Objects.equals(id, majorId)) {
            throw exception(MAJOR_DIRECTION_PARENT_ERROR);
        }
        // 2. 父专业方向不存在
        MajorDirectionDO parentMajorDirection = majorDirectionMapper.selectById(majorId);
        if (parentMajorDirection == null) {
            throw exception(MAJOR_DIRECTION_PARENT_NOT_EXITS);
        }
        // 3. 递归校验父专业方向，如果父专业方向是自己的子专业方向，则报错，避免形成环路
        if (id == null) { // id 为空，说明新增，不需要考虑环路
            return;
        }
        for (int i = 0; i < Short.MAX_VALUE; i++) {
            // 3.1 校验环路
            majorId = parentMajorDirection.getMajorId();
            if (Objects.equals(id, majorId)) {
                throw exception(MAJOR_DIRECTION_PARENT_IS_CHILD);
            }
            // 3.2 继续递归下一级父专业方向
            if (majorId == null || MajorDirectionDO.MAJOR_ID_ROOT.equals(majorId)) {
                break;
            }
            parentMajorDirection = majorDirectionMapper.selectById(majorId);
            if (parentMajorDirection == null) {
                break;
            }
        }
    }

    private void validateMajorDirectionDirectionNameUnique(Long id, Long majorId, String directionName) {
        MajorDirectionDO majorDirection = majorDirectionMapper.selectByMajorIdAndDirectionName(majorId, directionName);
        if (majorDirection == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的专业方向
        if (id == null) {
            throw exception(MAJOR_DIRECTION_DIRECTION_NAME_DUPLICATE);
        }
        if (!Objects.equals(majorDirection.getId(), id)) {
            throw exception(MAJOR_DIRECTION_DIRECTION_NAME_DUPLICATE);
        }
    }

    @Override
    public MajorDirectionDO getMajorDirection(Long id) {
        return majorDirectionMapper.selectById(id);
    }

    @Override
    public List<MajorDirectionDO> getMajorDirectionList(MajorDirectionListReqVO listReqVO) {
        return majorDirectionMapper.selectList(listReqVO);
    }

}